package com.atguigu.gmall.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.product.mapper.BaseCategoryViewMapper;
import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.product.model.BaseCategoryView;
import com.atguigu.gmall.product.service.BaseCategoryViewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BaseCategoryViewServiceImpl extends ServiceImpl<BaseCategoryViewMapper, BaseCategoryView> implements BaseCategoryViewService {

    /**
     * 查询所有分类列表,分类嵌套结果:一级分类对象中包含二级分类集合,二级分类对象中包含二级分类集合
     * @return
     */
    @Override
    @GmallCache(prefix = "BaseCategoryList")
    public List<JSONObject> getBaseCategoryList(){
        //1.构建所有一级集合分类对
        ArrayList<JSONObject> resultList = new ArrayList<>();
        //2.查询所有分类试图,获取所有分类集合
        List<BaseCategoryView> allCategorylist = this.list();
        if(!CollectionUtils.isEmpty(allCategorylist)){
            //3.对所有分类集合根据一级分类ID分组
            Map<Long, List<BaseCategoryView>> category1ListMap = allCategorylist.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
            int index = 1;
            //4.遍历分组后的map(获取一级分类id,获取一级分类名称)
            for (Map.Entry<Long, List<BaseCategoryView>> category1Entry : category1ListMap.entrySet()) {
                JSONObject category1 = new JSONObject();
                List<JSONObject> category2JsonList = new ArrayList<>();
                Long category1Id = category1Entry.getKey();
                String category1Name = category1Entry.getValue().get(0).getCategory1Name();
                //将获取的一级分类序号,id,名字封装到集合
                category1.put("index", index++);
                category1.put("categoryId", category1Id);
                category1.put("categoryName", category1Name);
                //5.处理当前一级分类中包含二级分类
                Map<Long, List<BaseCategoryView>> category2ListMap = category1Entry.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
                //5.1遍历二级分类map(获取二级分类id,获取二级分类名称)
                for (Map.Entry<Long, List<BaseCategoryView>> category2Entry : category2ListMap.entrySet()) {
                    JSONObject category2 = new JSONObject();
                    Long category2Id = category2Entry.getKey();
                    String category2Name = category2Entry.getValue().get(0).getCategory2Name();
                    //5.2封装二级分类json对象
                    category2.put("categoryId", category2Id);
                    category2.put("categoryName", category2Name);
                    category2JsonList.add(category2);

                    //6.处理二级分类中包含三级分类
                    ArrayList<JSONObject> category3JsonList = new ArrayList<>();
                    List<BaseCategoryView> category3List = category2Entry.getValue();
                    //6.1遍历三级分类集合,创建三级分类对象,将三级分类分类集合放入二级分类对象categoryChild
                    for (BaseCategoryView baseCategoryView : category3List) {
                        JSONObject category3 = new JSONObject();
                        category3.put("categoryId", baseCategoryView.getCategory3Id());
                        category3.put("categoryName",baseCategoryView.getCategory3Name());
                        category3JsonList.add(category3);
                    }
                    //将三级分类存到二级分类
                    category2.put("categoryChild",category3JsonList);
                }
                //将二级分类存到categoryChild属性
                category1.put("categoryChild", category2JsonList);
                //将一级分类存对象存到总集合
                resultList.add(category1);
            }
        }
        return resultList;
    }
}